package com.android.hdhe.uhf.reader;


import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class WeightReader{
    private static SerialPort serialPort;
    private static int port = 1;
    private static int baudRate = 9600;
    private static InputStream in;
    private static OutputStream os;
    private static WeightReader reader;
    static Handler mHandler;

    private WeightReader() {

    }

    public static WeightReader getInstance(Handler handler) {
        mHandler = handler;
        if (serialPort == null) {
            try {
                serialPort = new SerialPort(port, baudRate, 0);
            } catch (Exception var2) {
                return null;
            }
            serialPort.psampoweron();
            in = serialPort.getInputStream();
            os = serialPort.getOutputStream();
            isFlagSerial = true;
            receive();
        }
        try {
            Thread.sleep(200L);

        } catch (InterruptedException var1) {
            var1.printStackTrace();
        }
        if(reader==null){
            reader = new WeightReader();
        }
        return reader;
    }


    public static boolean isFlagSerial = false;
    public static Thread receiveThread = null;
    public static String strData = "";
    public static void receive() {
        if (receiveThread != null && !isFlagSerial) {
            return;
        }
        receiveThread = new Thread() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(20);
                while (isFlagSerial) {
                    try {
                        byte[] readData = new byte[1024];
                        if (in == null) {
                            return;
                        }
                        Log.d("wjk","before readData");
                        int size = in.read(readData);
                        Log.d("wjk","after readData"+size);
                        if (size > 0 && isFlagSerial) {
                            strData = byteToStr(readData, size);
                            Message message = mHandler.obtainMessage(33);
                            message.obj=strData;
                            mHandler.sendMessage(message);
                            strData = new String(readData, 0, size);
                        }
                    } catch (IOException e) {
                        Log.d("wjk","readData exception"+e.toString());
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        receiveThread.start();
    }

    /**
     * 接收到的字节数组转换16进制字符串
     */
    public static String byteToStr(byte[] b, int size) {
        String ret = "";
        for (int i = 0; i < size; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }

    public void powerOn() {
        serialPort.psampoweron();
    }

    public void powerOff() {
        serialPort.psampoweroff();
    }



    public void close() {
        isFlagSerial = false;
        try {
            in.close();
            os.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        }
        if (serialPort != null) {
            serialPort.psampoweroff();
            serialPort.close(port);
            serialPort = null;
        }
        if (reader != null) {
            reader = null;
        }
    }
}
